import re
import time
import urllib.parse
import urllib.request
import psutil
from PIL import Image

def getHtml(url: str) -> list:
    urls = list()
    html = urllib.request.urlopen(url).read().decode('utf-8')
    print('html:::', end='')
    print(html)
    saveHtml("www.jsu.edu.cn/zjxy1/xyfg.html", html, 'utf-8')
    urlJPGs: list = re.findall(r'<img.*src="(.*?)"', html)
    for urljpg in urlJPGs:
        new_full_url = urllib.parse.urljoin(url, urljpg)
        print(urljpg, " :  ", new_full_url)
        urls.append(new_full_url)
    return urls


def saveHtml(file_name, file_content, encoding):
    # 注意windows文件命名的禁用符，比如 /
    with open(file_name.replace('/', '_') + ".html", "w", encoding=encoding) as f:
        for i in file_content:
            f.write(i)


def saveJpg(ulrs):
    global sum
    # 注意windows文件命名的禁用符，比如 /
    for url in urls:
        html = urllib.request.urlopen(url).read()
        jpgfile = str(sum) + ".jpg"
        print('jpgfile  = ', jpgfile)
        with open(jpgfile, "wb") as f:
            f.write(html)
        sum = sum + 1

def dispJpgs():
    global sum
    sum=42
    process_list = []
    for proc in psutil.process_iter():
        process_list.append(proc)
    print('sum = ' + str(sum))
    for i in range(1, sum ):
        filename = str(i) + ".jpg"
        img = Image.open(filename)
        img.show()
        time.sleep(0.1)
        for proc in psutil.process_iter():
            if not proc in process_list:
                proc.kill()

if __name__ == '__main__':
    global sum
    sum = 1
    url = "http://www.jsu.edu.cn/zjxy1/xyfg.htm"
    urls = getHtml(url)
    saveJpg(urls)
    dispJpgs()